<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Member extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->is_logged_in();
        $this->load->model('member/member_model', 'member_model');
        $this->load->model('user/user_model', 'user_model');
    }

    public function index() {
    	$this->config->load('site', TRUE);
        $users = $this->member_model->find_all();
        $this->_data['members'] = $users;
        $this->_data['full_name'] = "";
        $this->_data['gender_value'] = "";
        $this->_data['owner_id'] = "";
        $this->_data['gender'] = $this->config->item('gender', 'site');
        $this->_data['status'] = $this->config->item('status', 'site');
        $this->_data['owners'] = $this->user_model->find_all( );
        $this->load->admintemplate('list', $this->_data);
    }
    public function search() {
    	$this->config->load('form_validate', TRUE);
    	$this->config->load('site', TRUE);
    	$this->_data['status'] = $this->config->item('status', 'site');
    	$params['full_name'] = $this->input->post('full_name');
    	$params['gender_value'] = $this->input->post('gender');
    	$params['owner_id'] = $this->input->post('owner_id');
    	$members = $this->member_model->getSearch($params, $sort = 'status', $order = 'asc');
    	$this->_data['members'] = $members;
    	$this->_data['gender'] = $this->config->item('gender', 'site');
        $this->_data['status'] = $this->config->item('status', 'site');
        $this->_data['owners'] = $this->user_model->find_all( );
        $this->_data['owner_id'] = $params['owner_id'];
        $this->_data['full_name'] = $params['full_name'];
        $this->_data['gender_value'] = $params['gender_value'];
    	$this->load->admintemplate('list', $this->_data);
    }
    public function add() {
        $this->load->library('form_validation');
        $this->config->load('form_validate', TRUE);
        $this->config->load('site', TRUE);
        $this->form_validation->set_rules($this->config->item('member', 'form_validate'));
        $this->_data['gender'] = $this->config->item('gender', 'site');
        $this->_data['status'] = $this->config->item('status', 'site');
        $this->_data['owners'] = $this->user_model->find_all( );
        if ($this->form_validation->run() == FALSE) {
            $this->load->admintemplate('add', $this->_data);
        } else {
            $params['gender'] = $this->input->post('gender', TRUE);
            $params['first_name'] = $this->input->post('first_name', TRUE);
            $params['last_name'] = $this->input->post('last_name', TRUE);
            $params['address'] = $this->input->post('address', TRUE);
            $params['house_number'] = $this->input->post('house_number', TRUE);
            $params['postcode'] = $this->input->post('postcode', TRUE);
            $params['place'] = $this->input->post('place', TRUE);
            $params['email'] = $this->input->post('email', TRUE);
            $params['phone'] = $this->input->post('phone', TRUE);
            $params['birthday'] = $this->input->post('birthday', TRUE);
            $params['owner'] = $this->input->post('owner', TRUE);
            $params['death'] = $this->input->post('death', TRUE);
            if($this->input->post('password', TRUE))
            	$params['password'] = $this->encode_pwd($this->input->post('password', TRUE));
            $this->member_model->insert($params);
            redirect('member');
        }
    }

    public function edit($id) {
        $this->_data['member'] = $this->member_model->find_id($id);
        $this->_data['owners'] = $this->user_model->find_all( );
        $this->load->library('form_validation');
        $this->config->load('form_validate', TRUE);
        $this->config->load('site', TRUE);
        $this->_data['gender'] = $this->config->item('gender', 'site');
        $this->_data['status'] = $this->config->item('status', 'site');
        $this->form_validation->set_rules($this->config->item('member', 'form_validate'));

        if ($this->form_validation->run() == FALSE) {
            $this->load->admintemplate('edit', $this->_data);
        } else {
            $params['gender'] = $this->input->post('gender', TRUE);
            $params['first_name'] = $this->input->post('first_name', TRUE);
            $params['last_name'] = $this->input->post('last_name', TRUE);
            $params['address'] = $this->input->post('address', TRUE);
            $params['house_number'] = $this->input->post('house_number', TRUE);
            $params['postcode'] = $this->input->post('postcode', TRUE);
            $params['place'] = $this->input->post('place', TRUE);
            $params['email'] = $this->input->post('email', TRUE);
            $params['phone'] = $this->input->post('phone', TRUE);
            $params['birthday'] = $this->input->post('birthday', TRUE);
            $params['owner'] = $this->input->post('owner', TRUE);
            $params['death'] = $this->input->post('death', TRUE);
            if($this->input->post('password', TRUE))
            	$params['password'] = $this->encode_pwd($this->input->post('password', TRUE));

            $this->member_model->update($id, $params);
            redirect('member');
        }
    }

    public function delete() {
        $id = $this->input->post('id');
        echo $this->member_model->delete($id);
        die;
    }

    public function user_check($str) {
        if ($str == 'test') {
            $this->form_validation->set_message('user_check', 'The %s field can not be the word "test"');
            return FALSE;
        } else {
            $this->form_validation->set_message('user_check', 'The %s field can not be the word "test"');
            return FALSE;
        }
    }

}
